<?php
/**
 * @link http://www.xbdla.com/
 * @copyright Copyright (c) 2016 小冰动力
 * @author ZhengChengJi<email:274543025@qq.com>
 * @description 后台菜单及权限
 * @version 1.0
 */

namespace backend\models;

use app\models\AdminGroup;
use common\models\User;
use yii;

class Menu
{
    /**
     * 权限配制数据
     * visible = false 时菜单不显示
     * @return array
     */
    private static function data()
    {
        $icon = 'fa-circle-o';
        $iconChild = 'fa-angle-double-right';
        return [
            ['label' => '内容管理', 'url' => ['post/index'], 'icon' => 'fa fa-edit', 'items' =>
                [
                    ['label' => '内容列表', 'url' => ['post/index'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '新建内容', 'url' => ['post/create'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '更新内容', 'url' => ['post/update'], 'visible' => false],
                    ['label' => '删除内容', 'url' => ['post/delete'], 'visible' => false],
                    ['label' => '上传组图', 'url' => ['post/upload-photos'], 'visible' => false],
                    ['label' => '内容属性', 'url' => ['attributes/index'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '新建属性', 'url' => ['attributes/create'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '更新属性', 'url' => ['attributes/update'], 'visible' => false],
                    ['label' => '删除属性', 'url' => ['attributes/delete'], 'visible' => false],
                ]
            ],
            ['label' => '栏目管理', 'url' => ['catalog/index'], 'icon' => 'fa fa-navicon', 'items' =>
                [
                    ['label' => '栏目列表', 'url' => ['catalog/index'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '新建栏目', 'url' => ['catalog/create'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '更新栏目', 'url' => ['catalog/update'], 'visible' => false],
                    ['label' => '删除栏目', 'url' => ['catalog/delete'], 'visible' => false],
                ]
            ],
            ['label' => '素材管理', 'url' => ['article/index'], 'icon' => 'fa fa-gift', 'items' =>
                [
                    ['label' => '素材列表', 'url' => ['article/index'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '新建素材', 'url' => ['article/create'], 'icon' => 'fa ' . $iconChild],
                    ['label' => '更新素材', 'url' => ['article/update'], 'visible' => false],
                    ['label' => '删除素材', 'url' => ['article/delete'], 'visible' => false],
                    ['label' => '上传组图', 'url' => ['article/upload-photos'], 'visible' => false],
                ]
            ],
            ['label' => '幻灯片管理', 'url' => ['banner/index'], 'icon' => 'fa fa-file-video-o', 'items' => [
                ['label' => '幻灯片列表', 'url' => ['banner/index'], 'icon' => 'fa fa-file-image-o'],
                ['label' => '新建幻灯片', 'url' => ['banner/create'], 'icon' => 'fa fa-file-image-o'],
                ['label' => '更新幻灯片', 'url' => ['banner/update'], 'visible' => false],
                ['label' => '删除幻灯片', 'url' => ['banner/delete'], 'visible' => false],
                ['label' => '图片列表', 'url' => ['banner/item-index'], 'visible' => false],
                ['label' => '新建图片', 'url' => ['banner/item-create'], 'visible' => false],
                ['label' => '更新图片', 'url' => ['banner/item-update'], 'visible' => false],
                ['label' => '删除图片', 'url' => ['banner/item-delete'], 'visible' => false],
            ]],
            ['label' => '留言管理', 'url' => ['message/index'], 'icon' => 'fa fa-comment-o', 'items' => [
                ['label' => '查看留言', 'url' => ['message/view'], 'visible' => false],
                ['label' => '删除留言', 'url' => ['message/delete'], 'visible' => false]
            ]],
            ['label' => '友情链接', 'url' => ['links/index'], 'icon' => 'fa fa-link', 'items' => [
                ['label' => '新建友情链接', 'url' => ['links/create'], 'visible' => false],
                ['label' => '更新友情链接', 'url' => ['links/update'], 'visible' => false],
                ['label' => '删除友情链接', 'url' => ['links/delete'], 'visible' => false]
            ]],
            ['label' => '站点配置', 'url' => ['config/index'], 'icon' => 'fa fa-cogs', 'items' =>
                [
                    ['label' => '基本设置', 'url' => ['config/index'], 'icon' => 'fa fa-cog'],
                    ['label' => '自定义设置', 'url' => ['config/custom'], 'icon' => 'fa fa-book'],
                    ['label' => '新建自定义', 'url' => ['config/custom-create'], 'visible' => false],
                    ['label' => '更新自定义', 'url' => ['config/custom-update'], 'visible' => false],
                    ['label' => '删除自定义', 'url' => ['config/custom-delete'], 'visible' => false],
                    ['label' => '视图管理', 'url' => ['view/index'], 'icon' => 'fa fa-road'],
                    ['label' => '新建视图', 'url' => ['view/create'], 'visible' => false],
                    ['label' => '更新视图', 'url' => ['view/update'], 'visible' => false],
                    ['label' => '删除视图', 'url' => ['view/delete'], 'visible' => false]
                ]
            ],
            ['label' => '自定义模块', 'url' => ['module/index'], 'icon' => 'fa fa-cubes', 'items' =>
                [
                    ['label' => '模块列表', 'url' => ['module/index'], 'icon' => 'fa fa-cube'],
                    ['label' => '新建模块', 'url' => ['module/create'], 'icon' => 'fa fa-cube'],
                    ['label' => '更新模块', 'url' => ['module/update'], 'visible' => false],
                    ['label' => '删除模块', 'url' => ['module/delete'], 'visible' => false],
                ]
            ],
            ['label' => '模板管理', 'url' => ['template/index'], 'icon' => 'fa fa-window-maximize', 'items' =>
                [
                    ['label' => '内容页', 'url' => ['template/post'], 'icon' => 'fa fa-tv'],
                    ['label' => '新建内容页', 'url' => ['template/post-create'], 'visible' => false],
                    ['label' => '更新内容页', 'url' => ['template/post-update'], 'visible' => false],
                    ['label' => '删除内容页', 'url' => ['template/post-delete'], 'visible' => false],
                    ['label' => '目录页', 'url' => ['template/catalog'], 'icon' => 'fa fa-tv'],
                    ['label' => '新建目录页', 'url' => ['template/catalog-create'], 'visible' => false],
                    ['label' => '更新目录页', 'url' => ['template/catalog-update'], 'visible' => false],
                    ['label' => '删除目录页', 'url' => ['template/catalog-delete'], 'visible' => false],
                    ['label' => '目录列表块', 'url' => ['template/item'], 'icon' => 'fa fa-table'],
                    ['label' => '新建列表块', 'url' => ['template/item-create'], 'visible' => false],
                    ['label' => '更新列表块', 'url' => ['template/item-update'], 'visible' => false],
                    ['label' => '删除列表块', 'url' => ['template/item-delete'], 'visible' => false],
                    ['label' => '首页', 'url' => ['template/home'], 'icon' => 'fa fa-tv'],
                    ['label' => '母版页', 'url' => ['template/layout'], 'icon' => 'fa fa-tv'],
                    ['label' => '新建母版页', 'url' => ['view/layout-create'], 'visible' => false],
                    ['label' => '更新母版页', 'url' => ['view/layout-update'], 'visible' => false],
                    ['label' => '删除母版页', 'url' => ['view/layout-delete'], 'visible' => false],
                    ['label' => '资源管理', 'url' => ['resources/index'], 'icon' => 'fa fa-folder-o'],
                ]
            ],
            ['label' => '用户管理', 'url' => ['admin/index'], 'icon' => 'fa fa-users', 'items' =>
                [
                    ['label' => '用户列表', 'url' => ['admin/index'], 'icon' => 'fa fa-user-o'],
                    ['label' => '新建用户', 'url' => ['admin/create'], 'visible' => false],
                    ['label' => '更新用户', 'url' => ['admin/update'], 'visible' => false],
                    ['label' => '删除用户', 'url' => ['admin/delete'], 'visible' => false],
                    ['label' => '权限管理', 'url' => ['admin/group-index'], 'icon' => 'fa fa-cogs'],
                    ['label' => '新建权限', 'url' => ['admin/group-create'], 'visible' => false],
                    ['label' => '更新权限', 'url' => ['admin/group-update'], 'visible' => false],
                    ['label' => '删除权限', 'url' => ['admin/group-delete'], 'visible' => false],
                    ['label' => '用户日志', 'url' => ['admin/log-index'], 'icon' => 'fa fa-book'],
                ]
            ],
            ['label' => '数据库管理', 'url' => ['dbmanage/index'], 'icon' => 'fa fa-database', 'items' => [
                ['label' => '备份', 'url' => ['dbmanage/backup'], 'icon' => 'glyphicon glyphicon-hdd'],
                ['label' => '还原', 'url' => ['dbmanage/restore'], 'icon' => 'glyphicon glyphicon-retweet']
            ]],
        ];
    }

    /**
     * 菜单过滤
     * @return array
     */
    public static function filterMenu()
    {
        $header = ['label' => '控制面板', 'options' => ['class' => 'header']];
        $aclList = self::data();
        $append = yii\helpers\Url::to(['site/index']);
        $adminId = Yii::$app->user->id;
        $admin = User::findOne($adminId);
        $group = AdminGroup::findOne($admin['group_id']);
        if ($group->id != 1) {
            $aclStr = rtrim($group->acl, ',') . ',' . $append;
            $aclArr = explode(',', $aclStr);
            foreach ($aclList as $k => $r) {
                $acl = $r['url'][0];
                if (!in_array($acl, $aclArr)) {
                    unset($aclList[$k]);
                } else {
                    if (isset($r['items'])) {
                        foreach ($r['items'] as $kk => $rr) {
                            $acl = $rr['url'][0];
                            if (!in_array($acl, $aclArr)) {
                                unset($aclList[$k]['items'][$kk]);
                            }
                        }
                    }
                }
            }
        }
        array_unshift($aclList, $header);
        return $aclList;
    }

    /**
     * 权限数据
     * @return array
     */
    public static function getList()
    {
        return self::data();
    }
}